package com.credithc.signature.facade.configure.jdbc;

import com.credithc.signature.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;
import java.util.Date;

@Slf4j
public class DatePreciseModuleShardingTableAlgorithm implements PreciseShardingAlgorithm<Date> {

    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {
        // 获取日志时间
        Date date = preciseShardingValue.getValue();
        String logicTableName = preciseShardingValue.getLogicTableName();
        String yearAndMonth = null;
        if (date == null) {
            yearAndMonth = DateUtil.getYearAndMonth(new Date());
        } else {
            yearAndMonth = DateUtil.getYearAndMonth(date);
        }
        String actualTableName = logicTableName + "_" + yearAndMonth;
        log.debug("当前SQL语句操作逻辑表为：{}:::实际表为：{}", logicTableName, actualTableName);
        return actualTableName;
    }
}
